In [1]:
#hide
!pip install -Uqq fastbook
import fastbook
fastbook.setup_book()
In [2]:
#hide
from fastbook import *

Your Deep Learning Journey

Deep Learning Is for Everyone

Neural Networks: A Brief History

Who We Are

How to Learn Deep Learning

Your Projects and Your Mindset

The Software: PyTorch, fastai, and Jupyter

Your First Model

Getting a GPU Deep Learning Server

Running Your First Notebook

In [3]:
# CLICK ME
from fastai.vision.all import *
path = untar_data(URLs.PETS)/'images'

def is_cat(x): return x[0].isupper()
dls = ImageDataLoaders.from_name_func(
    path, get_image_files(path), valid_pct=0.2, seed=42,
    label_func=is_cat, item_tfms=Resize(224))

learn = cnn_learner(dls, resnet34, metrics=error_rate)
learn.fine_tune(1)
epoch train_loss valid_loss error_rate time
0 0.161289 0.021331 0.009472 00:22
epoch train_loss valid_loss error_rate time
0 0.067894 0.041609 0.010149 00:28

Sidebar: This Book Was Written in Jupyter Notebooks

In [6]:
1+1
Out[6]:
2
In [7]:
img = PILImage.create(image_cat())
img.to_thumb(192)
Out[7]:

End sidebar

In [8]:
uploader = widgets.FileUpload()
uploader
In [ ]:
#hide
# For the book, we can't actually click an upload button, so we fake it
# uploader = SimpleNamespace(data = ['images/chapter1_cat_example.jpg'])
In [9]:
img = PILImage.create(uploader.data[0])
img
Out[9]:
In [10]:
#img = PILImage.create(uploader.data[0])
is_cat,_,probs = learn.predict(img)
print(f"Is this a cat?: {is_cat}.")
print(f"Probability it's a cat: {probs[1].item():.6f}")
Is this a cat?: True.
Probability it's a cat: 1.000000

What Is Machine Learning?

In [11]:
gv('''program[shape=box3d width=1 height=0.7]
inputs->program->results''')
Out[11]:
G program program results results program->results inputs inputs inputs->program
In [12]:
gv('''model[shape=box3d width=1 height=0.7]
inputs->model->results; weights->model''')
Out[12]:
G model model results results model->results inputs inputs inputs->model weights weights weights->model
In [13]:
gv('''ordering=in
model[shape=box3d width=1 height=0.7]
inputs->model->results; weights->model; results->performance
performance->weights[constraint=false label=update]''')
Out[13]:
G model model results results model->results inputs inputs inputs->model performance performance results->performance weights weights weights->model performance->weights update
In [14]:
gv('''model[shape=box3d width=1 height=0.7]
inputs->model->results''')
Out[14]:
G model model results results model->results inputs inputs inputs->model

What Is a Neural Network?

A Bit of Deep Learning Jargon

In [20]:
gv('''ordering=in
model[shape=box3d width=1 height=0.7 label=architecture]
inputs->model->predictions; parameters->model; labels->loss; predictions->loss
loss->parameters[constraint=false label=update]''')
Out[20]:
G model architecture predictions predictions model->predictions inputs inputs inputs->model loss loss predictions->loss parameters parameters parameters->model labels labels labels->loss loss->parameters update

Limitations Inherent To Machine Learning

From this picture we can now see some fundamental things about training a deep learning model:

  • A model cannot be created without data.
  • A model can only learn to operate on the patterns seen in the input data used to train it.
  • This learning approach only creates predictions, not recommended actions.
  • It's not enough to just have examples of input data; we need labels for that data too (e.g., pictures of dogs and cats aren't enough to train a model; we need a label for each one, saying which ones are dogs, and which are cats).

Generally speaking, we've seen that most organizations that say they don't have enough data, actually mean they don't have enough labeled data. If any organization is interested in doing something in practice with a model, then presumably they have some inputs they plan to run their model against. And presumably they've been doing that some other way for a while (e.g., manually, or with some heuristic program), so they have data from those processes! For instance, a radiology practice will almost certainly have an archive of medical scans (since they need to be able to check how their patients are progressing over time), but those scans may not have structured labels containing a list of diagnoses or interventions (since radiologists generally create free-text natural language reports, not structured data). We'll be discussing labeling approaches a lot in this book, because it's such an important issue in practice.

Since these kinds of machine learning models can only make predictions (i.e., attempt to replicate labels), this can result in a significant gap between organizational goals and model capabilities. For instance, in this book you'll learn how to create a recommendation system that can predict what products a user might purchase. This is often used in e-commerce, such as to customize products shown on a home page by showing the highest-ranked items. But such a model is generally created by looking at a user and their buying history (inputs) and what they went on to buy or look at (labels), which means that the model is likely to tell you about products the user already has or already knows about, rather than new products that they are most likely to be interested in hearing about. That's very different to what, say, an expert at your local bookseller might do, where they ask questions to figure out your taste, and then tell you about authors or series that you've never heard of before.

How Our Image Recognizer Works

What Our Image Recognizer Learned

Image Recognizers Can Tackle Non-Image Tasks

Jargon Recap

Deep Learning Is Not Just for Image Classification

In [21]:
path = untar_data(URLs.CAMVID_TINY)
dls = SegmentationDataLoaders.from_label_func(
    path, bs=8, fnames = get_image_files(path/"images"),
    label_func = lambda o: path/'labels'/f'{o.stem}_P{o.suffix}',
    codes = np.loadtxt(path/'codes.txt', dtype=str)
)

learn = unet_learner(dls, resnet34)
learn.fine_tune(8)
epoch train_loss valid_loss time
0 2.637429 2.316640 00:02
epoch train_loss valid_loss time
0 1.772902 1.583740 00:02
1 1.546775 1.238036 00:01
2 1.398717 1.058699 00:02
3 1.278426 0.973161 00:02
4 1.159765 0.825363 00:01
5 1.045720 0.742632 00:02
6 0.951408 0.711544 00:02
7 0.877272 0.710340 00:02
In [22]:
learn.show_results(max_n=6, figsize=(7,8))
In [23]:
from fastai.text.all import *

dls = TextDataLoaders.from_folder(untar_data(URLs.IMDB), valid='test')
learn = text_classifier_learner(dls, AWD_LSTM, drop_mult=0.5, metrics=accuracy)
learn.fine_tune(4, 1e-2)
epoch train_loss valid_loss accuracy time
0 0.602330 0.411419 0.812280 01:50
epoch train_loss valid_loss accuracy time
0 0.332169 0.244486 0.905520 03:32
1 0.258494 0.263969 0.894880 03:33
2 0.202468 0.185812 0.927720 03:33
3 0.159729 0.189003 0.930840 03:33

If you hit a "CUDA out of memory error" after running this cell, click on the menu Kernel, then restart. Instead of executing the cell above, copy and paste the following code in it:

from fastai.text.all import *

dls = TextDataLoaders.from_folder(untar_data(URLs.IMDB), valid='test', bs=32)
learn = text_classifier_learner(dls, AWD_LSTM, drop_mult=0.5, metrics=accuracy)
learn.fine_tune(4, 1e-2)

This reduces the batch size to 32 (we will explain this later). If you keep hitting the same error, change 32 to 16.

In [24]:
learn.predict("I really liked that movie!")
Out[24]:
('pos', TensorText(1), TensorText([3.6323e-05, 9.9996e-01]))

Sidebar: The Order Matters

End sidebar

In [30]:
from fastai.tabular.all import *
path = untar_data(URLs.ADULT_SAMPLE)

dls = TabularDataLoaders.from_csv(path/'adult.csv', path=path, y_names="salary",
    cat_names = ['workclass', 'education', 'marital-status', 'occupation',
                 'relationship', 'race'],
    cont_names = ['age', 'fnlwgt', 'education-num'],
    procs = [Categorify, FillMissing, Normalize])

learn = tabular_learner(dls, metrics=accuracy)
In [31]:
learn.fit_one_cycle(3)
epoch train_loss valid_loss accuracy time
0 0.364650 0.359323 0.829699 00:05
1 0.345817 0.354641 0.835381 00:05
2 0.343454 0.352362 0.835074 00:05
In [33]:
learn.show_results()
workclass education marital-status occupation relationship race education-num_na age fnlwgt education-num salary salary_pred
0 5.0 10.0 1.0 14.0 2.0 5.0 1.0 0.323349 -1.032615 1.138751 0.0 0.0
1 2.0 10.0 3.0 14.0 1.0 5.0 1.0 0.543325 -1.484729 1.138751 1.0 1.0
2 5.0 16.0 3.0 13.0 1.0 5.0 1.0 0.836625 -0.647268 -0.036484 1.0 1.0
3 6.0 8.0 7.0 0.0 2.0 5.0 1.0 0.616650 0.165143 0.747006 0.0 0.0
4 5.0 8.0 3.0 4.0 1.0 5.0 1.0 -0.703202 0.593521 0.747006 0.0 0.0
5 5.0 12.0 3.0 15.0 1.0 5.0 1.0 1.496551 -0.832995 -0.428229 0.0 0.0
6 5.0 16.0 1.0 13.0 2.0 5.0 1.0 0.543325 -1.237247 -0.036484 0.0 0.0
7 5.0 16.0 6.0 13.0 2.0 5.0 1.0 0.543325 -0.145264 -0.036484 0.0 0.0
8 5.0 12.0 5.0 15.0 4.0 5.0 1.0 -1.143152 -0.706601 -0.428229 0.0 0.0
In [34]:
from fastai.collab import *
path = untar_data(URLs.ML_SAMPLE)
dls = CollabDataLoaders.from_csv(path/'ratings.csv')
learn = collab_learner(dls, y_range=(0.5,5.5))
learn.fine_tune(10)
epoch train_loss valid_loss time
0 1.515585 1.397089 00:00
epoch train_loss valid_loss time
0 1.379971 1.337834 00:00
1 1.287778 1.157149 00:00
2 1.039614 0.857866 00:00
3 0.801019 0.723723 00:00
4 0.687387 0.692560 00:00
5 0.643663 0.684684 00:00
6 0.625596 0.680191 00:00
7 0.615936 0.677464 00:00
8 0.609623 0.676806 00:00
9 0.620369 0.676752 00:00
In [35]:
learn.show_results()
userId movieId rating rating_pred
0 80.0 49.0 5.0 3.692048
1 98.0 77.0 5.0 4.843116
2 7.0 18.0 4.0 4.290390
3 21.0 97.0 3.0 3.603601
4 48.0 10.0 2.0 2.332250
5 23.0 14.0 4.0 3.711537
6 43.0 58.0 4.0 3.851321
7 39.0 78.0 2.5 3.279262
8 68.0 96.0 4.0 3.887544

Sidebar: Datasets: Food for Models

End sidebar

Validation Sets and Test Sets

Use Judgment in Defining Test Sets

A Choose Your Own Adventure moment

Questionnaire

It can be hard to know in pages and pages of prose what the key things are that you really need to focus on and remember. So, we've prepared a list of questions and suggested steps to complete at the end of each chapter. All the answers are in the text of the chapter, so if you're not sure about anything here, reread that part of the text and make sure you understand it. Answers to all these questions are also available on the book's website. You can also visit the forums if you get stuck to get help from other folks studying this material.

  1. Do you need these for deep learning?

    • Lots of math T / F
    • Lots of data T / F
    • Lots of expensive computers T / F
    • A PhD T / F
  2. Name five areas where deep learning is now the best in the world.

  3. What was the name of the first device that was based on the principle of the artificial neuron?
  4. Based on the book of the same name, what are the requirements for parallel distributed processing (PDP)?
  5. What were the two theoretical misunderstandings that held back the field of neural networks?
  6. What is a GPU?
  7. Open a notebook and execute a cell containing: 1+1. What happens?
  8. Follow through each cell of the stripped version of the notebook for this chapter. Before executing each cell, guess what will happen.
  9. Complete the Jupyter Notebook online appendix.
  10. Why is it hard to use a traditional computer program to recognize images in a photo?
  11. What did Samuel mean by "weight assignment"?
  12. What term do we normally use in deep learning for what Samuel called "weights"?
  13. Draw a picture that summarizes Samuel's view of a machine learning model.
  14. Why is it hard to understand why a deep learning model makes a particular prediction?
  15. What is the name of the theorem that shows that a neural network can solve any mathematical problem to any level of accuracy?
  16. What do you need in order to train a model?
  17. How could a feedback loop impact the rollout of a predictive policing model?
  18. Do we always have to use 224×224-pixel images with the cat recognition model?
  19. What is the difference between classification and regression?
  20. What is a validation set? What is a test set? Why do we need them?
  21. What will fastai do if you don't provide a validation set?
  22. Can we always use a random sample for a validation set? Why or why not?
  23. What is overfitting? Provide an example.
  24. What is a metric? How does it differ from "loss"?
  25. How can pretrained models help?
  26. What is the "head" of a model?
  27. What kinds of features do the early layers of a CNN find? How about the later layers?
  28. Are image models only useful for photos?
  29. What is an "architecture"?
  30. What is segmentation?
  31. What is y_range used for? When do we need it?
  32. What are "hyperparameters"?
  33. What's the best way to avoid failures when using AI in an organization?

Further Research

Each chapter also has a "Further Research" section that poses questions that aren't fully answered in the text, or gives more advanced assignments. Answers to these questions aren't on the book's website; you'll need to do your own research!

  1. Why is a GPU useful for deep learning? How is a CPU different, and why is it less effective for deep learning?
  2. Try to think of three areas where feedback loops might impact the use of machine learning. See if you can find documented examples of that happening in practice.
In [ ]: